<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模板页面</title>
    <!-- 引入样式 -->
<!--    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">-->
<!--    <link rel="stylesheet" type="text/css" href="./static/css/elementuifont.css"/>-->
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="../../static/js/Vue-2.6.12.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/js/axios.min.js" type="text/javascript" charset="utf-8"></script>
    <!--    <script src="./static/js/elementui.js" type="text/javascript" charset="utf-8"></script>-->

    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>

    <style>
        .el-row {
            margin-bottom: 20px;
        &:last-child {
             margin-bottom: 0;
         }
        }
        .el-col {
            border-radius: 4px;
        }
        .grid-content {
            border-radius: 4px;
            min-height: 36px;
        }
        .row-bg {
            padding: 10px 0;
            background-color: #f9fafc;
        }
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body>
    <div id="app">

        <el-row>
            <el-col :span="3"><div class="grid-content"></div></el-col>
        </el-row>
        <el-row>
            <el-col :span="3"><div class="grid-content"></div></el-col>
            <el-col :span="18">
                <el-form :inline="true" :model="searForm" class="demo-form-inline">
                    <el-form-item>
                        <el-input clearable v-model="searForm.name" placeholder="分类名称"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-input clearable v-model="searForm.isShow" placeholder="是否显示"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="findList">查询</el-button>
                        <el-button type="success" @click="dialogFormVisible = true,category = {}">新增</el-button>
                    </el-form-item>
                </el-form>

                <el-table
                        :data="dataList"
                        border
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="id"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="分类名"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="goodsNum"
                            label="商品数量">
                    </el-table-column>
                    <el-table-column
                            label="是否显示">
                        <template  slot-scope="scope">
                            <el-switch
                                    v-model="scope.row.isShow"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949"
                                    active-value="1"
                                    inactive-value="0"
                                    disabled>
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column
                            label="是否导航">
                        <template  slot-scope="scope">
                            <el-switch
                                    v-model="scope.row.isMenu"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949"
                                    active-value="1"
                                    inactive-value="0"
                                    disabled>
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column
                            label="设置">
                        <template slot-scope="scope">
                            <el-link type="danger" @click="findParentId(scope.row.id)" v-show="parentIds.length<3">查看下级</el-link>
                            <el-link type="danger" @click="goback" v-show="parentIds.length>1">返回上级</el-link>
                        </template>
                    </el-table-column>
                    <el-table-column
                            label="操作">
                        <template slot-scope="scope">
                            <el-link type="primary" @click="getbyid(scope.row.id)">编辑</el-link>
                            <el-link type="danger" @click="del(scope.row.id)">删除</el-link>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                        @size-change="findList"
                        @current-change="findList"
                        :current-page.sync="currentPage"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size.sync="pagesize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </el-col>
            <el-col :span="3"><div class="grid-content bg-purple"></div></el-col>
        </el-row>

        <!-- 新增窗口  -->
        <el-dialog title="保存" :visible.sync="dialogFormVisible">
            <el-form :model="category">
                <el-form-item label="分类名" :label-width="formLabelWidth">
                    <el-input v-model="category.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="是否展示" :label-width="formLabelWidth">
                    <el-switch
                            v-model="category.isShow"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            active-value="1"
                            inactive-value="0">
                    </el-switch>
                </el-form-item>
                <el-form-item label="是否导航" :label-width="formLabelWidth">
                    <el-switch
                            v-model="category.isMenu"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            active-value="1"
                            inactive-value="0">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</body>

<script type="text/javascript">
    new Vue({
        el:"#app",
        data:{
            dataList:[],
            currentPage:1,
            pagesize:10,
            total:100,
            searForm:{parentId:0},
            dialogFormVisible:false,
            category:{},
            formLabelWidth:"180px",
            parentIds:[0]
        },
        created(){
            this.findList()
        },
        methods:{
            findList(){
                axios.post(`/category/findconditionpage.do?startpage=${this.currentPage}&pagesize=${this.pagesize}`, this.searForm).then((res) => {
                    this.dataList = res.data.records
                    this.total = res.data.total
                })
            },
            save(){
                let id =  this.category.id
                let action = id?'update':'save';
                this.category.parentId = this.parentIds[this.parentIds.length-1]
                axios.post(`/category/${action}.do`,this.category).then((res)=>{
                    if(res.data.code=="200"){
                        this.dialogFormVisible = false
                        this.$message({
                            type: 'success',
                            message: res.data.msg
                        });
                        this.findList()
                    }else{
                        this.$message.error(res.data.msg)
                    }
                })
            },
            del(id){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get(`/category/delbyid.do?id=${id}`).then((res)=>{
                        if(res.data.code=="200"){
                            this.dialogFormVisible = false
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.findList()
                        }else{
                            this.$message.error(res.data.msg + " "+ res.data.ex )
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            getbyid(id){
                axios.get(`/category/findbyid.do?id=${id}`).then((res)=>{
                    this.dialogFormVisible = true
                    this.$message({
                        type: 'success',
                        message: '查询成功!'
                    });
                    this.category = res.data
                })
            },
            findParentId(id){
                this.searForm.parentId = id
                this.parentIds.push(id)
                this.currentPage = 1
                this.findList()
            },
            goback(){
                this.parentIds.pop()
                this.searForm.parentId = this.parentIds[this.parentIds.length-1]
                this.findList()
            }
        }
    })
</script>
</html>